<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ?'部门编辑' :'添加部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @closed="hClose"
      >
        <dept-dialog
          :id="id"
          ref="deptDialogRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartDetail } from '@/api/departments'
import { tranListTotreeList } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      id: '',
      isEdit: false, // false为添加,true为编辑
      originList: []
    }
  },
  async created() {
    await this.loadDepartments()
  },
  methods: {
    // 重置表单
    hClose() {
      this.$refs.deptDialogRef.resetForm()
    },
    // 删除部门
    async hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确认
        try {
          const res = await delDepartDetail(id)
          console.log(res)
          if (res.success) {
            this.$message.success('删除成功')
          }
          // 重新加载页面
          this.loadDepartments()
        } catch (err) {
          console.log(err)
        }
      }).catch(() => {
        // 取消
      })
    },
    // 加载组织架构列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        this.originList = res.data.depts.map(item => {
          const { name, id, pid, code } = item
          return { name, id, pid, code }
        })
        // shift() 返回值为数组的第一个
        const company = res.data.depts.shift()
        console.log(company, 'company')
        console.log(res.data.depts)
        this.list = tranListTotreeList(res.data.depts)
        console.log(this.list)
      } catch (error) {
        console.log('错误', error)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.isEdit = false
      this.id = id
    },
    // 成功添加部门后关闭对话框并加载数据
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
      this.$message.success('成功')
    },
    // 编辑
    hEdit(id) {
      this.showDialog = true
      this.id = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.deptDialogRef.loadDepartDetail()
      })
    }
  }
}
</script>
